﻿#include "MEMSDataHandler.h"

#include "MEMSDataCodec.h"
#include  "LogSaver.h"
#include "qdebug.h"


MEMSDataHandler::MEMSDataHandler(QObject* parent) :QObject(parent)
{
	qRegisterMetaType<QVector<float>>("QVector<float>");
	clearAllGroup();
	isListen = false;
	connect(&timer_collection, &QTimer::timeout, this, &MEMSDataHandler::slot_timer_ave_timeOut);
}
MEMSDataHandler::~MEMSDataHandler()
{
	timer_collection.disconnect();
	disconnect();
}
void MEMSDataHandler::slot_handleData(int dataGroupIndex, QByteArray data)
{
	if (!isListen)
	{
		return;
	}
	logSaver->saveLog(QStringLiteral("%1：收到第%2组的数据：%3").arg(objectName()).arg(dataGroupIndex + 1).arg((QString)data.toHex()), LogSaver::Debug);
	switch (dataCollectionProtocol)
	{
	case Protocol_SingleTemperature:
		calc_singleTemperature(dataGroupIndex, data);
		break;
	case Protocol_DoubleTemperature:
		calc_doubleTemperature(dataGroupIndex, data);
		break;
	default:
		break;
	}
}
void MEMSDataHandler::slot_startDataCollection()
{
	if (isListen)
	{
		return;
	}
	isListen = true;
	logSaver->saveLog(QStringLiteral("%1：开始数据采集").arg(objectName()), LogSaver::Information);
	clearAllGroup();
	timer_collection.start(interval_collection);
	emit signal_currentDataCollectionState(isListen);
}
void MEMSDataHandler::slot_stopDataCollection()
{
	if (!isListen)
	{
		return;
	}
	isListen = false;
	logSaver->saveLog(QStringLiteral("%1：停止数据采集").arg(objectName()), LogSaver::Information);
	timer_collection.stop();
	emit signal_currentDataCollectionState(isListen);
	clearAllGroup();
}
void MEMSDataHandler::calc_singleTemperature(int dataGroupIndex, QByteArray data)
{
	if (!dataGroup_float.contains(dataGroupIndex))
	{
		QVector<float> temp;
		for (int j = 0; j < 7; ++j)
		{
			temp.append(0);
		}
		dataGroup_float.insert(dataGroupIndex, temp);
		count_dataNumber.insert(dataGroupIndex, 0);
	}
	++count_dataNumber[dataGroupIndex];
	for (int i = 0; i < 7; ++i) //给前七个float数据位赋对应的值
	{
		int index = i * 2;
		dataGroup_float[dataGroupIndex][i] = calcAve(
			dataGroup_float[dataGroupIndex][i],
			(float)char2ToShort(data.at(index), data.at((index + 1))),
			(float)count_dataNumber[dataGroupIndex]);

	}
}
void MEMSDataHandler::protocol_singleTemperature(int dataGroupIndex)
{
	logSaver->saveLog(QStringLiteral("%1：处理完第%2组的数据").arg(objectName()).arg(dataGroupIndex + 1), LogSaver::Debug);
	emit signal_dataHandled(dataGroupIndex, dataGroup_float[dataGroupIndex], Protocol_SingleTemperature);
	clearGroup(dataGroupIndex);
}

void MEMSDataHandler::calc_doubleTemperature(int dataGroupIndex, QByteArray data)
{
	if (!dataGroup_float.contains(dataGroupIndex))
	{
		QVector<float> temp;
		for (int j = 0; j < 8; ++j)
		{
			temp.append(0);
		}
		dataGroup_float.insert(dataGroupIndex, temp);
		count_dataNumber.insert(dataGroupIndex, 0);
	}
	++count_dataNumber[dataGroupIndex];
	for (int i = 0; i < 6; ++i)
	{//给前六个float数据位赋对应的值
		int index = i * 2;
		dataGroup_float[dataGroupIndex][i] = calcAve(
			dataGroup_float[dataGroupIndex][i],
			(float)char2ToShort(data.at(index), data.at((index + 1))),
			(float)count_dataNumber[dataGroupIndex]);
	}
	dataGroup_float[dataGroupIndex][6] = calcAve(
		dataGroup_float[dataGroupIndex][6],
		(float)(data.at(12) > 127 ? (data.at(12) - 256) : data.at(12)),
		(float)count_dataNumber[dataGroupIndex]);
	dataGroup_float[dataGroupIndex][7] = calcAve(
		dataGroup_float[dataGroupIndex][7],
		(float)(data.at(13) > 127 ? data.at(13) - 256 : data.at(13)),
		(float)count_dataNumber[dataGroupIndex]);
}
void MEMSDataHandler::protocol_doubleTemperature(int dataGroupIndex)
{
	logSaver->saveLog(QStringLiteral("%1：处理完第%2组的数据").arg(objectName()).arg(dataGroupIndex + 1), LogSaver::Debug);
	emit signal_dataHandled(dataGroupIndex, dataGroup_float[dataGroupIndex], Protocol_DoubleTemperature);
	clearGroup(dataGroupIndex);
}

void MEMSDataHandler::processingData(int dataGroupIndex)
{
	switch (dataCollectionProtocol)
	{
	case Protocol_SingleTemperature:
		protocol_singleTemperature(dataGroupIndex);
		break;
	case Protocol_DoubleTemperature:
		protocol_doubleTemperature(dataGroupIndex);
		break;
	default:
		break;
	}
}
void MEMSDataHandler::clearAllGroup()//重置各数据位到初始状态
{
	count_dataNumber.clear();
	dataGroup_float.clear();
}
void MEMSDataHandler::clearGroup(int groupIndex)
{
	count_dataNumber.remove(groupIndex);
	dataGroup_float.remove(groupIndex);
}
short MEMSDataHandler::char2ToShort(char H8, char L8)//两char转short，解析数据时使用
{
	short data1 = 0;
	data1 = ((data1 | H8) << 8) | (L8 & 0xff);
	return data1;
}

float MEMSDataHandler::calcAve(float oldSum, float newValue, float numCount)
{
	if (numCount <= 1)
	{
		qDebug() << QStringLiteral("1ave:%1").arg(newValue);
		return newValue;
	}
	else
	{
		qDebug() << QStringLiteral("2ave:%1").arg(oldSum * ((numCount - 1) / numCount) + (newValue / numCount));
		return oldSum * ((numCount - 1) / numCount) + (newValue / numCount);
	}
}

void MEMSDataHandler::slot_timer_ave_timeOut()//计时器触发函数
{
	QList<int> keyList = dataGroup_float.keys();
	std::sort(keyList.begin(), keyList.end());
	for (int i : keyList)
	{
		processingData(i);
	}
}

int MEMSDataHandler::getInterval_collection()
{
	return interval_collection;
}
void MEMSDataHandler::setInterval_collection(int interval_collection)
{
	this->interval_collection = interval_collection;
}
bool MEMSDataHandler::getIsListen()
{
	return isListen;
}
void MEMSDataHandler::setIsListen(bool isListen)
{
	this->isListen = isListen;
}

MEMSDataHandler::DataCollectionProtocol MEMSDataHandler::getDataCollectionProtocol()
{
	return dataCollectionProtocol;
}
void MEMSDataHandler::setDataCollectionProtocol(DataCollectionProtocol dataCollectionProtocol)
{
	this->dataCollectionProtocol = dataCollectionProtocol;
	emit signal_currentDataCollectionProtocol(dataCollectionProtocol);
}

MEMSDataCodec* MEMSDataHandler::getMEMSDataCodec()
{
	return mEMSDataCodec;
}
void MEMSDataHandler::setMEMSDataCodec(MEMSDataCodec* mEMSDataCodec)
{
	if (Q_NULLPTR != this->mEMSDataCodec)
	{
		disconnect(this->mEMSDataCodec, &MEMSDataCodec::signal_feedbackData, this, &MEMSDataHandler::slot_handleData);
	}
	this->mEMSDataCodec = mEMSDataCodec;
	connect(this->mEMSDataCodec, &MEMSDataCodec::signal_feedbackData, this, &MEMSDataHandler::slot_handleData);
}
LogSaver* MEMSDataHandler::getLogSaver()
{
	return logSaver;
}
void MEMSDataHandler::setLogSaver(LogSaver* logSaver)
{
	this->logSaver = logSaver;
}
